package com.csd.activity;

import com.csd.fragment.EnterpriseAllFragment;
import com.csd.fragment.AuditingFragment;
import com.csd.fragment.OrderFragment;
import com.csd.fragment.OutCarFragment;
import com.csd.fragment.ReturnCarFragment;
import com.csd.fragment.SelectCarFragment;
import com.csd.view.MenuPopupwindow;
import com.csd.view.PagerSlidingTabStrip;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class EnterpriseOrderActivity extends FragmentActivity implements OnClickListener  {

	/**
	 * 获取当前屏幕的密度
	 */
	private DisplayMetrics dm;
	private ActionBar actionbar;
	private PagerSlidingTabStrip tabs;
	private EnterpriseAllFragment allFragment;
	private AuditingFragment auditingFragment;
	private SelectCarFragment selectFragment;
	private OutCarFragment outCarFrament;
	private ReturnCarFragment returnFragment;
	private NavigationAdapter adapter;
	private ViewPager pager;
	private CsdApplication csdApp;
	private String str[] = {"首页","消息"};
	private ImageView msg;

	@Override
	protected void onCreate(Bundle savedInstanceState)  {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_enterpriseorder);
		initView();
	}

	@SuppressLint("NewApi")
	private void initView() {
		// TODO Auto-generated method stub
		if(csdApp == null){
			csdApp = CsdApplication.getInstance();
		}
		csdApp.addActivity(this);
		actionbar = getActionBar();
		actionbar.setDisplayHomeAsUpEnabled(false);
		actionbar.setDisplayShowHomeEnabled(false);
		actionbar.setDisplayShowTitleEnabled(false);
		actionbar.setDisplayShowCustomEnabled(true);
		LayoutInflater inflator = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

		View v = inflator.inflate(R.layout.actionbar_main, null);
		TextView title = (TextView) v.findViewById(R.id.title);
		title.setText("企业订单");
		View line = v.findViewById(R.id.line);
		line.setVisibility(View.GONE);
		msg = (ImageView) v.findViewById(R.id.msg);
		msg.setImageDrawable(getResources().getDrawable(R.drawable.u88));
		msg.setOnClickListener(this);
		ImageView img = (ImageView) v.findViewById(R.id.logo);
		img.setImageDrawable(getResources().getDrawable(R.drawable.u86));
	
		img.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			Intent intent = new Intent(EnterpriseOrderActivity.this, MainActivity.class);	
			intent.putExtra("num", 1);
			intent.putExtra("flag", 0);
			startActivity(intent);
			csdApp.exit();
			}
		});
		ActionBar.LayoutParams layout = new ActionBar.LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		actionbar.setCustomView(v, layout);
		dm = getResources().getDisplayMetrics();
		pager = (ViewPager) findViewById(R.id.pager);
		tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
		adapter = new NavigationAdapter(getSupportFragmentManager());
		pager.setAdapter(adapter);
		pager.setSelected(true);
		Intent intent = getIntent();
		int i = intent.getIntExtra("num", 0);
		pager.setCurrentItem(i);
		tabs.setViewPager(pager);
		setTabsValue();
		
	}
	/**
	 * 对PagerSlidingTabStrip的各项属性进行赋值。
	 */
	private void setTabsValue() {
		// 设置Tab是自动填充满屏幕的
		tabs.setShouldExpand(true);
		// 设置Tab的分割线是透明的
		tabs.setDividerColor(Color.TRANSPARENT);
		// 设置Tab底部线的高度
		tabs.setUnderlineHeight((int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 1, dm));
		// 设置Tab Indicator的高度
		tabs.setIndicatorHeight((int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 4, dm));
		// 设置Tab标题文字的大小
		tabs.setTextSize((int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_SP, 14, dm));
		// 设置Tab Indicator的颜色
		tabs.setIndicatorColor(Color.parseColor("#FF6600"));
		// 设置选中Tab文字的颜色 (这是我自定义的一个方法)
//		tabs.setSelectedTextColor(Color.parseColor("#FF6600"));
		// 取消点击Tab时的背景色
	}

	private class NavigationAdapter extends FragmentPagerAdapter {

		public NavigationAdapter(FragmentManager fm) {
			super(fm);
			// TODO Auto-generated constructor stub
		}

		private final String[] titles = { "全部", "待审核", "待选车", "待出车", "待还车" };

		@Override
		public CharSequence getPageTitle(int position) {
			return titles[position];
		}

		@Override
		public int getCount() {
			return titles.length;
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0:
				if (allFragment == null) {
					allFragment = new EnterpriseAllFragment();
				}
				return allFragment;
			case 1:
				if (auditingFragment == null) {
					auditingFragment = new AuditingFragment();
				}
				return auditingFragment;
			case 2:
				if (selectFragment == null) {
					selectFragment = new SelectCarFragment();
				}
				return selectFragment;
			case 3:
				if (outCarFrament == null) {
					outCarFrament = new OutCarFragment();
				}
				return outCarFrament;
			case 4:
				if (returnFragment == null) {
					returnFragment = new ReturnCarFragment();
				}
				return returnFragment;
			default:
				return null;
			}
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.msg:
			MenuPopupwindow menu = new MenuPopupwindow(this);
			menu.showPopupWindow(msg);
			break;

		default:
			break;
		}
	}

}
